package com.quasar.hpatient.module.comm_album_photo;

import android.Manifest;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.comm_album.AlbumFolderBean;
import com.quasar.hpatient.bean.comm_album.AlbumPhotoBean;
import com.quasar.hpatient.module.comm_album.AlbumActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import lib.quasar.base.frame.BaseFragment;
import lib.quasar.context.BaseConstant;
import lib.quasar.permission.annotation.PermissionAgain;
import lib.quasar.permission.annotation.PermissionDenied;
import lib.quasar.permission.annotation.PermissionFail;
import lib.quasar.permission.annotation.PermissionSucc;
import lib.quasar.permission.core.PermissionManager;
import lib.quasar.permission.core.intent.IntentType;
import lib.quasar.util.LogUtil;
import lib.quasar.util.ToastUtil;

/**
 * description: 选择图片, 图片集合
 * created by kalu on 2018/4/18 15:54
 */
public class AlbumPhotoFragment extends BaseFragment<AlbumPhotoPresenter> implements AlbumPhotoView {

    @Override
    public int initView() {
        return R.layout.fragment_comm_album_photo;
    }

    @Override
    public void initDataLocal() {

        View view = getView();
        if (null == view) return;

        final boolean hasCamera = getActivity().getIntent().getBooleanExtra(AlbumActivity.CAMERA, false);
        final RecyclerView recycler = getView().findViewById(R.id.fragment_album_photo_recycler);
        getPresenter().initLayoutData(this, recycler, hasCamera);
    }

    @Override
    public void initDataNet() {
    }

    @Override
    public void setPhotoList(Map<String, AlbumFolderBean> map, String folder) {
        final boolean hasCamera = getActivity().getIntent().getBooleanExtra(AlbumActivity.CAMERA, false);
        final RecyclerView recycler = getView().findViewById(R.id.fragment_album_photo_recycler);
        getPresenter().photoList(this, recycler, map, folder, hasCamera);
    }

    @Override
    public void setResult() {
        final ArrayList<AlbumPhotoBean> list = getPresenter().getSelectPhoto();
        AlbumActivity activity = (AlbumActivity) getActivity();
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(AlbumActivity.LIST, list);
        activity.setResult(AlbumActivity.RESULT_CODE, intent);
    }

    @Override
    public void goCamera() {

        PermissionManager.get(AlbumPhotoFragment.this)
                .setForce(true)
                .setPermissionName(Manifest.permission.CAMERA)
                .setRequestCode(BaseConstant.PERMISSION_CAMERA)
                .setPageType(IntentType.GOOGLE_SETTING)
                .request();
    }

    @PermissionSucc(BaseConstant.PERMISSION_CAMERA)
    public void cameraSucc(int code, List<String> list) {
        LogUtil.e("dsds", "cameraSucc ==> code = " + code);
        ToastUtil.showToast("获取相机权限成功");
        getActivity().setResult(AlbumActivity.RESULT_CODE_OPEN_CAMERA);
        getActivity().onBackPressed();
    }

    @PermissionFail(BaseConstant.PERMISSION_CAMERA)
    public void cameraFail(int code, List<String> list) {
        LogUtil.e("dsds", "cameraFail ==> code = " + code);
        ToastUtil.showToast("获取相机权限失败");
    }

    @PermissionAgain(BaseConstant.PERMISSION_CAMERA)
    public void cameraAgain(int code, List<String> list) {
        LogUtil.e("dsds", "cameraAgain ==> code = " + code);
    }

    @PermissionDenied(BaseConstant.PERMISSION_CAMERA)
    public void onDenied(int code, List<String> list, Intent intent) {
        LogUtil.e("dsds", "onDenied ==> code = " + code);
        ToastUtil.showToast("获取相机权限, 用户已拒绝");
        goNext(intent);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        PermissionManager.onRequestPermissionsResult(getActivity(), requestCode, grantResults);
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}